<%@ page language="java" pageEncoding="UTF-8"%>
<script type="text/javascript">
	var invitationDatagrid;
	var invitationForumTree;
	$(function() {
		invitationForumTree = $('#forum_invitationList_forumTree').tree({
			url : '${pageContext.request.contextPath}/treeAction!queryAllTreeNode.action?type=03',
			parentField : 'pid',
			lines : true,
			onClick : function(node) {
				invitationDatagrid.datagrid('load', {
					forumZoneId : node.id
				});
			}
		});

		invitationDatagrid = $('#forum_invitationList_datagrid').datagrid({
			url : '${pageContext.request.contextPath}/invitationAction!queryInvitationList.action',
			fit : true,
			fitColumns : true,
			border : false,
			striped : true,
			pagination : true,
			idField : 'id',
			pageSize : 10,
			pageList : [ 10, 20 ],
			sortName : 'updateTime',
			sortOrder : 'desc',
			checkOnSelect : true,
			selectOnCheck : true,
			singleSelect : true,
			striped : true,
			//rowStyler : function(index, row) {
			//	return 'height: 35px;';
			//},
			columns : [ [ {
				field : 'forumZoneName',
				title : '论坛分区名称',
				width : 80,
				hidden : true
			}, {
				field : 'title',
				title : '主题',
				width : 350,
				align : 'left',
				sortable : true,
				formatter : function(value, row, index) {
					return '<span title="'+value+'">' + value + '</span>';
				}
			}, {
				field : 'creator',
				title : '作者',
				width : 80,
				align : 'left',
				sortable : true
			}, {
				field : 'replyNum',
				title : '回复',
				width : 80,
				align : 'center'
			}, {
				field : 'readNum',
				title : '阅读',
				width : 80,
				align : 'center'
			}, {
				field : 'updaterName',
				title : '更新者',
				width : 80,
				align : 'left'
			}, {
				field : 'updateTime',
				title : '最近更新时间',
				width : 140,
				align : 'center',
				sortable : true
			} ] ],
			toolbar : [ {
				text : '我要发帖',
				iconCls : 'icon-add',
				handler : function() {
					addInvitation();
				}
			}, {
				text : '我要回帖',
				iconCls : 'icon-add',
				handler : function() {
					replyInvitation();
				}
			} ]
		});

		invitationDatagrid.datagrid('load', {
			forumZoneId : selectForumZoneId
		});

		function addInvitation() {
			$('#forum_invitationAdd_addInvitationForm input').val('');
			$('#forum_invitationAdd_addInvitationForm textarea').val('');
			var selectNode = invitationForumTree.tree('getSelected');
			if (selectNode != null) {
				var flag = invitationForumTree.tree('isLeaf', selectNode.target);
				if (flag) {
					var dlg = $('<div style="padding:10px 20px;" />').dialog({
						title : '我要发帖',
						width : 580,
						height : 350,
						href : '${pageContext.request.contextPath}/pages/forum/invitationAdd.jsp',
						modal : true,
						buttons : [ {
							text : '保存',
							handler : function() {
								$('#forum_invitationAdd_addInvitationForm').form('submit', {
									url : '${pageContext.request.contextPath}/invitationAction!addInvitation.action',
									success : function(r) {
										var obj = jQuery.parseJSON(r);
										//var obj = eval('('+r+')');
										if (obj.success) {
											invitationDatagrid.datagrid('insertRow', {
												index : 0,
												row : obj.obj
											});
											dlg.dialog('close');
											$.messager.show({
												title : '提示',
												msg : obj.msg
											});
										} else {
											$.messager.alert('错误', obj.msg);
										}
									}
								});
							}
						}, {
							text : '取消',
							handler : function() {
								dlg.dialog('close');
							}
						} ],
						onBeforeClose : function() {
							KindEditor.remove('textarea[name="content"]');
						},
						onClose : function() {
							$(this).dialog('destroy');
						},
						onLoad : function() {
							KindEditor.create('textarea[name="content"]', {
								resizeType : 1,
								allowPreviewEmoticons : false,
								allowImageUpload : false,
								items : [ 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline', 'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist', 'insertunorderedlist', '|', 'emoticons' ],
								afterBlur : function() {
									this.sync();
								}
							});
							$('#forum_invitationAdd_addInvitationForm input[name=forumZoneId]').val(selectNode.id);
						}
					});
				} else {
					$.messager.alert('提示', '请选择叶子节点！');
				}
			} else {
				$.messager.alert('提示', '请选择论坛分区！');
			}
		}

		function replyInvitation() {
			$('#forum_invitationReply_replyInvitationForm textarea').val('');
			var selectNode = invitationDatagrid.datagrid('getSelected');
			if (selectNode != null) {
				$.ajax({
					type : 'post',
					url : '${pageContext.request.contextPath}/invitationAction!queryInvitationAndReplyList.action',
					data : {
						id : selectNode.id
					},
					dataType : 'json',
					success : function(r) {
						var dlg = $('<div style="padding:10px 20px;" />').dialog({
							width : 800,
							height : 600,
							href : '${pageContext.request.contextPath}/pages/forum/invitationReply.jsp',
							modal : true,
							title : '我要回帖',
							buttons : [ {
								text : '保存',
								handler : function() {
									$('#forum_invitationReply_replyInvitationForm').form('submit', {
										url : '${pageContext.request.contextPath}/invitationReplyAction!addInvitationReply.action',
										success : function(r) {
											var obj = jQuery.parseJSON(r);
											if (obj.success) {
												$.messager.show({
													title : '提示',
													msg : obj.msg
												});
												dlg.dialog('close');
											} else {
												$.messager.alert('错误', obj.msg);
											}
										}
									});
								}
							}, {
								text : '取消',
								handler : function() {
									dlg.dialog('close');
								}
							} ],
							onBeforeClose : function() {
								KindEditor.remove('textarea[name="content"]');
							},
							onClose : function() {
								$(this).dialog('destroy');
							},
							onLoad : function() {
								KindEditor.create('textarea[name="content"]', {
									resizeType : 1,
									allowPreviewEmoticons : false,
									allowImageUpload : false,
									items : [ 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline', 'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist', 'insertunorderedlist', '|', 'emoticons' ],
									afterBlur : function() {
										this.sync();
									}
								});
								$('#forum_invitationReply_replyInvitationForm input[name=invitationId]').val(selectNode.id);
							}
						});
					}
				});
			} else {
				$.messager.alert('提示', '请选择要回复的帖子！');
			}
		}

		/*
		这是一种方式，下面是第二种方式
		$('.datagrid-header div').css('textAlign', 'center');
		$('.datagrid-header div').css('height', '35px');
		$('.datagrid-header div').css('lineHeight', '35px');
		 */
		$('.datagrid-header div').css({
			'textAlign' : 'center',
		//'height' : '35px',
		//'lineHeight' : '35px'
		});

	});
</script>
<div class="easyui-layout" data-options="fit:true,border:false">
	<div data-options="region:'west',border:false" style="width:200px; border-right-width: 1px;">
		<div class="easyui-panel" data-options="title:'论坛分区',fit:true,border:false" style="padding:5px 2px 2px 2px">
			<ul id="forum_invitationList_forumTree"></ul>
		</div>
	</div>
	<div data-options="region:'center',border:false">
		<table id="forum_invitationList_datagrid"></table>
	</div>
</div>
